package com.lianniu.web.tea.supply.service.impl;

import java.util.Date;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.lianniu.web.base.BaseResult;
import com.lianniu.web.tea.supply.dao.TeaSupplyProductDao;
import com.lianniu.web.tea.supply.dao.TeaSupplyProductPicDao;
import com.lianniu.web.tea.supply.model.TeaSupplyProduct;
import com.lianniu.web.tea.supply.model.TeaSupplyProductPic;
import com.lianniu.web.tea.supply.service.ITeaSupplyService;
import com.lianniu.web.util.MapUtil;
import com.lianniu.web.util.TeaContant;

@Service
@Transactional
public class TeaSupplyServiceImpl implements ITeaSupplyService {

	private final static Logger logger = Logger.getLogger(TeaSupplyServiceImpl.class);
	
	@Autowired
	private TeaSupplyProductDao teaSupplyProductDao;
	
	@Autowired
	private TeaSupplyProductPicDao teaSupplyProductPicDao;
	
	@Override
	public BaseResult saveSupply1(TeaSupplyProductPic productPic, TeaSupplyProduct product) {
		if(product.getProductid()==null){ //茶旅联盟
			//新增
			beforeSave(productPic,product);
			teaSupplyProductPicDao.insert(productPic);
			product.setPicid(productPic.getPicid());
			product.setCateid(Long.valueOf(TeaContant.TEA_PAY_PRODUCT_TYPE_TEA_CLUB));
			teaSupplyProductDao.insert(product);
			
		}else{
			//修改
			update(productPic,product);
		}
		return BaseResult.success(product.getProductid());
	}
	
	
	private void update(TeaSupplyProductPic productPic, TeaSupplyProduct product) {
		TeaSupplyProduct supplyProduct = teaSupplyProductDao.selectByPrimaryKey(product.getProductid());
		productPic.setPicid(supplyProduct.getPicid());
		
		String coverPic = productPic.getCoverPic();
		String[] split = coverPic.split(",");
		productPic.setPicturepath1(split[0]); //设置封面照
		productPic.setDelflag(TeaContant.TEA_PAY_SUPPLY_DELETE_FALSE);
		
		product.setStatus(TeaContant.TEA_PAY_SUPPLY_STATUS_FALSE);
		product.setDelflag(TeaContant.TEA_PAY_SUPPLY_DELETE_FALSE);
		
		teaSupplyProductPicDao.updateByPrimaryKeySelective(productPic);
		teaSupplyProductDao.updateByPrimaryKeySelective(product);
	}


	public void beforeSave(TeaSupplyProductPic productPic, TeaSupplyProduct product){
		//新增
		String coverPic = productPic.getCoverPic();
		String[] split = coverPic.split(",");
		productPic.setPicturepath1(split[0]); //设置封面照
		productPic.setDelflag(TeaContant.TEA_PAY_SUPPLY_DELETE_FALSE);
		
		productPic.setAddtime(new Date());
		
		String lon = product.getTeaProLon();
		String lat = product.getTeaProLat();
		
		String[] addName = MapUtil.getAddName(lon, lat);
		String location = "";
		if(addName[0].contains("黑龙江")){
			location = addName[0].substring(0, 3)+"/";
		}else{
			location = addName[0].substring(0, 2)+"/";
		}
		location = location+addName[1].substring(0, 2)+"/";
		location = location+addName[2].substring(0, 2);
		product.setTeaLocation(location);
		product.setStatus(TeaContant.TEA_PAY_SUPPLY_STATUS_FALSE);
		product.setDelflag(TeaContant.TEA_PAY_SUPPLY_DELETE_FALSE);
		product.setAddtime(new Date());
		product.setTeaSellNum(Long.valueOf("0"));
	}

	@Override
	public BaseResult saveSupply3(TeaSupplyProductPic productPic, TeaSupplyProduct product) {
		if(product.getProductid()==null){ //茶山
			//新增
			beforeSave(productPic,product);
			teaSupplyProductPicDao.insert(productPic);
			product.setPicid(productPic.getPicid());
			product.setCateid(Long.valueOf(TeaContant.TEA_PAY_PRODUCT_TYPE_TEA_GARDEN));
			teaSupplyProductDao.insert(product);
			
		}else{
			//修改
			update(productPic,product);
		}
		return BaseResult.success(product.getProductid());
	}

	@Override
	public BaseResult saveSupply4(TeaSupplyProductPic productPic, TeaSupplyProduct product) {
		if(product.getProductid()==null){ //茶人茶艺
			//新增
			beforeSave(productPic,product);
			teaSupplyProductPicDao.insert(productPic);
			product.setPicid(productPic.getPicid());
			product.setCateid(Long.valueOf(TeaContant.TEA_PAY_PRODUCT_TYPE_TEA_SKILL));
			teaSupplyProductDao.insert(product);
		}else{
			//修改
			update(productPic,product);
		}
		return BaseResult.success(product.getProductid());
	}


	@Override
	public BaseResult releaseInf(TeaSupplyProductPic productPic, TeaSupplyProduct product) {
		if(product.getProductid()==null){ //茶咨询
			//新增
			beforeSave(productPic,product);
			teaSupplyProductPicDao.insert(productPic);
			product.setPicid(productPic.getPicid());
			product.setCateid(Long.valueOf(TeaContant.TEA_PAY_PRODUCT_TYPE_TEA_INF));
			teaSupplyProductDao.insert(product);
			
		}else{
			//修改
			update(productPic,product);
		}
		return BaseResult.success(product.getProductid());
	}


	@Override
	public BaseResult saveDemand1(TeaSupplyProduct product) {
		if(product.getProductid()==null){ //茶旅联盟
			//新增
			beforeSaveDemand(product);
			product.setCateid(Long.valueOf(TeaContant.TEA_PAY_PRODUCT_TYPE_TEA_DEMAND_SKILL));
			teaSupplyProductDao.insert(product);
			
		}else{
			//修改
			teaSupplyProductDao.updateByPrimaryKeySelective(product);
		}
		return BaseResult.success(product.getProductid());
	}


	@Override
	public BaseResult saveDemand2(TeaSupplyProduct product) {
		if(product.getProductid()==null){ //茶旅联盟
			//新增
			beforeSaveDemand(product);
			product.setCateid(Long.valueOf(TeaContant.TEA_PAY_PRODUCT_TYPE_TEA_DEMAND_GARDEN));
			teaSupplyProductDao.insert(product);
			
		}else{
			//修改
			teaSupplyProductDao.updateByPrimaryKeySelective(product);
		}
		return BaseResult.success(product.getProductid());
	}
	
	public void beforeSaveDemand(TeaSupplyProduct product){
		String lon = product.getTeaProLon();
		String lat = product.getTeaProLat();
		
		String[] addName = MapUtil.getAddName(lon, lat);
		String location = "";
		if(addName[0].contains("黑龙江")){
			location = addName[0].substring(0, 3)+"/";
		}else{
			location = addName[0].substring(0, 2)+"/";
		}
		location = location+addName[1].substring(0, 2)+"/";
		location = location+addName[2].substring(0, 2);
		product.setTeaLocation(location);
		product.setStatus(TeaContant.TEA_PAY_SUPPLY_STATUS_FALSE);
		product.setDelflag(TeaContant.TEA_PAY_SUPPLY_DELETE_FALSE);
		product.setAddtime(new Date());
		product.setTeaSellNum(Long.valueOf("0"));
	}
}
